package com.gt.aep.web.systemManage;

import com.alibaba.fastjson.JSONObject;
import com.gt.aep.base.entity.systemManage.SysPermission;
import com.gt.aep.base.entity.util.DateTime;
import com.gt.aep.base.entity.util.Result;
import com.gt.aep.base.service.systemManage.SysPermissionIdentityRoleService;
import com.gt.aep.base.service.systemManage.SysPermissionService;
import com.gt.aep.base.service.systemManage.SysPermissionSysRoleService;
import com.gt.aep.base.util.EncryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@RequestMapping("sysPermission")
@Controller
public class SysPermissionController {
    @Autowired
    SysPermissionService sysPermissionService;
    @Autowired
    SysPermissionSysRoleService sysPermissionSysRoleService;
    @Autowired
    SysPermissionIdentityRoleService sysPermissionIdentityRoleService;

    /**
     * 新增或修改系统权限
     * @param data
     * @param encryptAesKey
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public Map<String, Object> save(String data, String encryptAesKey){
        //解密前端发送过来的数据
        String jsPublicKey = null;
        Map<String, Object> decData = EncryptUtil.decode(data,encryptAesKey);
        String jsAesKey = (String) decData.get("jsAesKey");
        String decrypt = (String) decData.get("data");
        //将String类型数据转化为对象
        JSONObject dataObject = new JSONObject(JSONObject.parseObject(decrypt));
        //根据前端的字段获取相对应的属性
        jsPublicKey = (String) dataObject.get("jsPublicKey");
        SysPermission sysPermission = (SysPermission) dataObject.get("sysPermission");
        String loginUid = (String) dataObject.get("loginUid");
        //判断是修改还是新增角色
        if(StringUtils.isEmpty(sysPermission.getId())){
            sysPermission.setId(UUID.randomUUID().toString());
            sysPermission.setCreateTime(DateTime.time());
            sysPermissionService.insert(sysPermission);
            return Result.success("新增权限成功");
        }else{
            sysPermission.setUpdater(loginUid);
            sysPermission.setUpdateTime(DateTime.time());
            sysPermissionService.update(sysPermission);
            return Result.success("编辑权限成功");
        }
    }

    @ResponseBody
    @RequestMapping("deletePermission")
    public Map<String, Object> deleteUser(String data,String encryptAesKey){
        //解密前端发送过来的数据
        String jsPublicKey = null;
        Map<String, Object> decData = EncryptUtil.decode(data,encryptAesKey);
        String jsAesKey = (String) decData.get("jsAesKey");
        String decrypt = (String) decData.get("data");
        //将String类型数据转化为对象
        JSONObject dataObject = new JSONObject(JSONObject.parseObject(decrypt));
        //根据前端的字段获取相对应的属性
        jsPublicKey = (String) dataObject.get("jsPublicKey");
        String[] ids = (String[]) dataObject.get("ids");
        //数据库操作
        int j = sysPermissionService.delete(ids);
        if (j>0){
            //删除关系表
            for (int i = 0; i < ids.length; i++) {
                try {
                    //删除权限-系统角色关系
                    sysPermissionSysRoleService.deleteByPid(ids[i]);
                    //删除权限-身份角色关系
                    sysPermissionIdentityRoleService.deleteByPid(ids[i]);
                } catch (Exception e) {
                    return Result.error("删除角色-用户关系失败");
                }
            }
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }

    @ResponseBody
    @RequestMapping("getPermissionByUid")
    public Map<String, Object> getPermissionByUid(String data, String encryptAesKey){
        //解密前端发送过来的数据
        String jsPublicKey = null;
        Map<String, Object> decData = EncryptUtil.decode(data,encryptAesKey);
        String jsAesKey = (String) decData.get("jsAesKey");
        String decrypt = (String) decData.get("data");
        //将String类型数据转化为对象
        JSONObject dataObject = new JSONObject(JSONObject.parseObject(decrypt));
        //根据前端的字段获取相对应的属性
        jsPublicKey = (String) dataObject.get("jsPublicKey");
        String loginUid = (String) dataObject.get("loginUid");

        List<Map<String, Object>> sysPermissionList = sysPermissionService.getPermissionByUserId(loginUid);
        return EncryptUtil.encryptData(sysPermissionList,jsPublicKey);
    }

    @ResponseBody
    @RequestMapping("getAllPermsiion")
    public Map<String, Object> getAllPermsiion(String data, String encryptAesKey){
        //解密前端发送过来的数据
        String jsPublicKey = null;
        Map<String, Object> decData = EncryptUtil.decode(data,encryptAesKey);
        String jsAesKey = (String) decData.get("jsAesKey");
        String decrypt = (String) decData.get("data");
        //将String类型数据转化为对象
        JSONObject dataObject = new JSONObject(JSONObject.parseObject(decrypt));
        //根据前端的字段获取相对应的属性
        jsPublicKey = (String) dataObject.get("jsPublicKey");

        List<SysPermission> sysPermissionList = sysPermissionService.getList();
        return EncryptUtil.encryptData(sysPermissionList,jsPublicKey);
    }


}
